<?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\UploadedFile;
/**
 * This is the model class for table "xa_enclosure".
 *
 * @property integer $id
 * @property string $xa_article_id
 * @property string $xa_type
 * @property string $xa_title
 * @property string $xa_name
 * @property string $xa_abstract
 * @property string $xa_create_time
 * @property string $xa_update_time
 * @property string $xa_remark
 */
class Enclosure extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'xa_enclosure';
    }

    public function behaviors() {
        return [
            [
                'class'=>TimestampBehavior::className(),
                'attributes'=>[
                    //创建时间
                    ActiveRecord::EVENT_BEFORE_INSERT=>['xa_create_time','xa_update_time'],
                    //修改时间
                    ActiveRecord::EVENT_BEFORE_UPDATE=>['xa_update_time']

                ],
                //设置默认值
                'value'=>date('Y-m-d H:i:s')

            ]

        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['xa_article_id', 'xa_create_time', 'xa_update_time'], 'string', 'max' => 100],
             [['xa_name'],'file','maxFiles' => 100],
            [['xa_type','xa_title', 'xa_abstract', 'xa_remark'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'xa_article_id' => '文章编号',
            'xa_type' => '附件类型',
            'xa_name' => '附件名称',
            'xa_title' => '附件标题',
            'xa_abstract' => '附件简介',
            'xa_create_time' => '创建时间',
            'xa_update_time' => '更新时间',
            'xa_remark' => '备注',
        ];
    }

    public function upload()
    {
        $this->xa_name = UploadedFile::getInstances($this, 'xa_name');
        $path = 'upload/file/'.date ( 'Ymd'); // 接收文件目录
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        if($this->xa_name!=null){
            if ($this->validate()) {
                foreach ($this->xa_name as $file) {

                    $file->saveAs($path.'/' . $file->baseName . '.' . $file->extension);
                }
                return true;
            }else {
                return false;
            }
        }
        return true;
    }

    public function Preservation()
    {
        //var_dump($this->xa_name->baseName);die;
        if (!$this->validators) {
            return false;
        }
        $path = 'upload/file/'.date ( 'Ymd');
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }

        if ($this->xa_name!=null)
        {
            if ($this->xa_name)
            {
                foreach ($this->xa_name as $file)
                {
                    $a[] = Yii::$app->request->hostInfo . '/'.$path.'/' . UploadedFile::getInstance($this, 'xa_name').$file->baseName . '.' . $file->extension;

                }
            }
            $pic=join(',',$a);
            $this->xa_name=$pic;

        }else{
            $this->xa_name = Enclosure::findOne($this->id)->xa_name;
        }

        if ($this->save()) {
            return true;
        }
        return false;

    }






}
